package com.czy.seed.mvc.home.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.czy.seed.mvc.base.service.impl.BaseServiceImpl;
import com.czy.seed.mvc.home.entity.Merchant;
import com.czy.seed.mvc.home.entity.PurchaseDetailedList;
import com.czy.seed.mvc.home.entity.Quotation;
import com.czy.seed.mvc.home.entity.QuoteOrCompare;
import com.czy.seed.mvc.home.mapper.QuotationMapper;
import com.czy.seed.mvc.home.service.MerchantService;
import com.czy.seed.mvc.home.service.PurchaseDetailedListService;
import com.czy.seed.mvc.home.service.QuotationService;
import com.czy.seed.mvc.home.service.QuoteOrCompareService;
import com.czy.seed.mvc.util.PrincipalUtil;
import com.czy.seed.mybatis.base.QueryParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: liuyun
 * Created by liuyun  11:47 2017/12/6.
 */
@Service
public class QuotationServiceImpl extends BaseServiceImpl<Quotation> implements QuotationService {

    @Autowired
    private QuotationMapper quoteMapper;
    @Autowired
    private QuotationService quotationService;

    @Autowired
    private QuoteOrCompareService quoteOrCompareService;
    @Autowired
    private PurchaseDetailedListService purchaseDetailedListService;
    @Autowired
    private MerchantService merchantService;
    @Override
    public void updateQuotationById(Long id,Long type,  Long c_userid) {
        quoteMapper.updateQuotationById(id,type, c_userid);
    }

    @Override
    public void updateQuotationOrCompaerByQuotationId(Long id,Long type,Long c_userid) {
        quoteMapper.updateQuotationOrCompaerByQuotationId(id,type,c_userid);
    }
    @Override
    public String addQuotation_impl(HttpServletRequest request){
        String ord = request.getParameter("quotation");
        String msg = "";
        try {
            Quotation quotation = JSONObject.parseObject(ord, Quotation.class);
            quotation.setMerchantId(PrincipalUtil.getLoginUser().getId());
            quotation.setQuoteUnit(PrincipalUtil.getLoginUser().getName());
            quotation.setQuoteTime(new Date());
            quotation.setCreatetime(new Date());
            quotation.setCreateuserid(PrincipalUtil.getLoginUser().getId());
            quotationService.insert(quotation);
            quoteOrCompareService.updateQuotationNumber(quotation.getQuoteOrCompareId(),PrincipalUtil.getLoginUser().getId());
            msg = "添加成功";
        } catch (Exception e) {
            msg = "添加失败！";
            throw new RuntimeException("添加报价单信息出错");
        }
        return msg;
    }
    @Override
    @Transactional
    public Map<String,Object>updateTypeByQuotationId_impl(Long id, int ifshow){//TODO 报价后的报价单信息
        Map<String, Object> pageInfo = new HashMap<String, Object>(16);
        try {
            //根据报价单ID查询出实体
            Quotation quotation1 = quotationService.selectByPrimaryKey(id);
            quotation1.setIfshow(ifshow);
            quotationService.updateByPrimaryKey(quotation1);
            long merchant_id = quotation1.getMerchantId();
            long quoteOrCompare_id = quotation1.getQuoteOrCompareId();
            long purchaseDetaileid = purchaseDetailedListService.selectPurchaseOrdesIdByQuotation(quoteOrCompare_id);
            //根据询价单ID找到询价单再由询价单找到清单，然后更新
            //根据商家ID再去找商家表中的ID
            QueryParams queryParams = new QueryParams(Merchant.class);
            QueryParams.Criteria criteria = queryParams.createCriteria();
            criteria.andEqualTo("userId",merchant_id);
            Merchant merchant_ = merchantService.selectOneByParams(queryParams);
            purchaseDetailedListService.updateByParentId4mmerchant(merchant_.getId(), purchaseDetaileid,PrincipalUtil.getLoginUser().getId(),quotation1.getQuoteSum());

            //依据报价单ID查询这个报价单的询价单ID

            long quoteOrCompareId = quotation1.getQuoteOrCompareId();
            //依据询价单ID更新所有姊妹单状态
            quotationService.updateQuotationOrCompaerByQuotationId(quoteOrCompareId, 1L,PrincipalUtil.getLoginUser().getId());
            quotationService.updateQuotationById(id, 8L,PrincipalUtil.getLoginUser().getId());
            //删除询价单中的临时数据
            QueryParams queryParams1 = new QueryParams(Quotation.class);
            QueryParams.Criteria criteria1 = queryParams1.createCriteria();
            criteria1.andEqualTo("quoteOrCompareId", quoteOrCompareId);
            criteria1.andIsNull("quoteSum");
            criteria1.andIsNull("quoteName");
            criteria1.andIsNull("commodityMakPrc");
            criteria1.andIsNull("priceCap");
            quotationService.deleteByParams(queryParams1);
            //依据询价单ID更新询价单状态
            quoteOrCompareService.updateQuotationOrCompaer(quoteOrCompareId, 1L,PrincipalUtil.getLoginUser().getId());
            //更新报价单状态
            pageInfo.put("flag", "true");
        } catch (Exception e) {
            throw new RuntimeException("依据报价单id更新姊妹单和主询价单状态失败");
        }
        return pageInfo;
    }

}
